//查看购物车
var customerId = $(".customerid").val();
getAll();
function getAll() {

    $.ajax({
        type:"GET",
        url:"/fresh/cart/getAll/"+customerId,
        dataType:"json",
        success:function (result) {
            console.log(result);
            if (result.status==1){
                var productList = result.data;

                //说明成功  进行页面的渲染
                $(".product-rows").html("");
                var str="";
                $.each(productList,function (index,productImage) {
                    var productId = productImage.product.productId;
                    console.log("productId:"+productId);
                    var productName = productImage.product.productName;
                    console.log("productName:"+productName);
                    var productPrice = productImage.product.productPrice;
                    console.log("productPrice:"+productPrice);
                    var image = productImage.stringList[0];
                    console.log("image:"+image);

                    var productNum = productImage.product.productNum;

                    var ele = " <li>\n" +
                        "                        <input type=\"hidden\" class=\"productId\" name=\"productId\"value="+productId+">\n" +
                        "                    <div class=\"product-fist\">\n" +
                        "                        <div class=\"select-product\">\n" +
                        "                            <input type=\"checkbox\">\n" +
                        "                        </div>\n" +
                        "                        <div class=\"product-messages\">\n" +
                        "                            <a href=\"\" class=\"photo\"><img src='/fresh/images/"+image+"'></a>\n" +
                        "                            <a href=\"\" class=\"mess\"><span class=\"product-name\">"+productName+" </span></a>\n" +
                        "                        </div>\n" +
                        "\n" +
                        "                        <div class=\"product-price\">\n" +
                        "                            <i class=\"amount-money\">￥</i>\n" +
                        "                            <i class=\"amount-money\">"+productPrice+"</i>\n" +
                        "                        </div>\n" +
                        "                        <div class=\"product-amount\">\n" +
                        "                            <input type=\"button\" value=\"-\" class=\"b1\">\n" +
                        "                            <input type=\"text\" value=\"1\">\n" +
                        "                            <input type=\"button\" value=\"+\" class=\"b2\">\n" +
                        "                        </div>\n" +
                        "                        <div class=\"price-sum\">\n" +
                        "                            <i class=\"sum-money\">￥</i>\n" +
                        "                            <i class=\"sum-money\">"+productPrice+"</i>\n" +
                        "                        </div>\n" +
                        "                        <div class=\"operation-product\">\n" +
                        "                            <a href=\"javascript:attentionProduct("+customerId+","+productId+");;\"class=\"to-collect\">收藏</a>\n" +
                        "                            <!--<input type=\"button\" value=\"删除\">-->\n" +
                        "                            <a href=\"javascript:deleteProduct("+productId+");\" class=\"delete-btn\">删除</a>\n" +
                        "\n" +
                        "                        </div>\n" +
                        "\n" +
                        "\n" +
                        "\n" +
                        "                    </div>\n" +
                        "                </li>"

                    str +=ele;
       // var lele="<a href=\"\" class=\"photo\"><img src=\"${pageContext.request.contextPath}/images\"></a>"
                })

                $(".product-rows").html(str);
            }
        }
    })
}

//删除方法
function deleteProduct(productId) {
    console.log("customerId:"+customerId);
    console.log("productId:"+productId);
    $.ajax({
        type: "GET",
        url:"/fresh/cart/delete/"+customerId+"/"+productId,
        dataType: "json",
        success:function (result) {
            console.log("result:"+result);
            if (result.status==1){
                console.log("删除成功");
            }else {
                console.log("删除失败");
            }
        }
    })
    getAll();
}
//关注
function attentionProduct(customerId,productId) {
    console.log("customerId:"+customerId);
    console.log("productId:"+productId);
    $.ajax({
        type: "GET",
        url: "/fresh/attention/add/"+customerId+"/"+productId,
        dataType: "json",
        success:function (result) {
            console.log("result："+result);
            if (result.status==1){
                console.log("收藏成功");
            }else {
                console.log("收藏失败");
            }
        }
    })
}

//获得购物车中的数量
getCountFormCart();
function getCountFormCart() {

    $.ajax({
        type:"GET",
        url:"/fresh/cart/getCount/"+customerId,
        dataType:"json",
        success:function (result) {
            if (result.status==1){
                console.log("查询成功");
                $(".product-num").html(result.data);
            }else {
                console.log("查询失败");
            }
        }
    })
}


//获取地址详情表
getAddress();
function getAddress() {

    $.ajax({
        type:"GET",
        url:"/fresh/address/getAll/"+customerId,
        dataType:"json",
        success:function (result) {

            var addressList = result.data;
            if (result.status==1){
                console.log("查询成功");
                $(".address-select").html("");
                var str = "";
                $.each(addressList,function (index,address) {
                    var addressId = address.addressId;
                    var addressName = address.addressName;
                    var addressCust = address.addressCust;
                    var addressProvince = address.addressProvince;
                    var addressCity = address.addressCity;
                    var addressArea = address.addressArea;
                    var addressDetails = address.addressDetails;
                    var addressPhone = address.addressPhone;
                    var ele = "<option value="+addressId+"><a href=\"javascript:;\">"+addressName+"</a></option>"

                    str +=ele;
                })

                $(".address-select").html(str);

            }else {
                console.log("查询失败");
            }

        }
    })
}


//获取地址id
$(".address-select").click(function () {
    var addressId = $(".address-select").val();
    console.log("id:"+addressId);
    //再将id复制给隐藏域
    $(".addressId").val(addressId);
})


//当点击结算按钮的时候 就清空购物车中的信息
$(".submit-btn").click(function () {
    $.ajax({
        type:"GET",
        url:"/fresh/cart/clear/"+customerId,
        dataType:"json",
        success:function (result) {
        if (result.status==1){
            console.log("清空成功");
        }else {
            console.log("清空失败");
        }

        }
    })
})

//当点击结算按钮的时候 进行支付

$(".submit-btn").click(function () {
    //循环
    var ckboxes = document.querySelectorAll(".item-single input[type=checkbox]");
    var str2 ="";

    var xiaoji ="";
    for (var i = 0; i<ckboxes.length;i++){
        //    遍历整个数组，查询所有的ckbox的状态
        if (ckboxes[i].checked == true){
            var price = ckboxes[i].parentElement.parentElement.children[4].lastElementChild.innerHTML; //单价
            var productId = ckboxes[i].parentElement.parentElement.parentElement.children[0].value;
            console.log("id"+productId);
            var num = ckboxes[i].parentElement.parentElement.children[3].children[1].value;
             xiaoji = price * num;  //小计
            console.log("num"+num);
            var str = productId+":"+num +";";
            str2 += str;
        }


    }

    var zongjia = $(".price-price").html();
    console.log("总价："+zongjia);


    var addressId = $(".addressId").val();
    console.log("地址id："+addressId);
    $.ajax({
        type:"post",
        url:"/fresh/alipay/createOrder",
        data:{
            "str":str2,
            "customerId":customerId,
            "addressId":addressId,
            "price":zongjia
        },
        dataType:"json",
        success:function (result) {
            console.log("778555");

            if (result.status==1){
                console.log("456");
                // 提交订单成功后, 进入购买页面
                window.location.href = "/fresh/alipay/goPay?orderId=" + result.data;
            }
        }
    })

})

//

function f1() {


}
